package com.yang;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

//发送带有附件的邮件
public class MailDemo03 {
    public static void main(String[] args) throws Exception {
        Properties prop=new Properties();
        prop.setProperty("mail.host","smtp.qq.com");///设置QQ邮件服务器
        prop.setProperty("mail.transport.protocol","smtp");///邮件发送协议
        prop.setProperty("mail.smtp.auth","true");//需要验证用户密码
        //QQ邮箱需要设置SSL加密
        MailSSLSocketFactory sf=new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable","true");
        prop.put("mail.smtp.ssl.socketFactory",sf);
    
    //使用javaMail发送邮件的5个步骤
    //1.创建定义整个应用程序所需要的环境信息的session对象
    Session session=Session.getDefaultInstance(prop, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("424793597@qq.com","exqapujkngifbjgf");
        }
    });
    //开启session的debug模式，这样可以查看到程序发送Email的运行状态
    session.setDebug(true);
    //2.通过session得到transport对象
    Transport ts=session.getTransport();
    //3.使用邮箱的用户名和授权码连上邮件服务器
    ts.connect("smtp.qq.com","424793597@qq.com","exqapujkngifbjgf");
    //4.连接上之后我们需要发送邮件
    MimeMessage mimeMessage = imageMail(session);
    //5.发送邮件
    ts.sendMessage(mimeMessage,mimeMessage.getAllRecipients());

    //6.关闭连接
    ts.close();
	}
	public static MimeMessage imageMail(Session session) throws MessagingException {
        //消息的固定信息
        MimeMessage message=new MimeMessage(session);
        //指明邮件的发件人
        message.setFrom(new InternetAddress("424793597@qq.com"));
        //指明邮件的收件人
        message.setRecipient(Message.RecipientType.TO,new InternetAddress("424793597@qq.com"));
        //邮件标题
        message.setSubject("java发出");

        /*
        编写内容：
            图片
            附件
            文本
         */
        //=================================准备图片数据===================================
        MimeBodyPart body1=new MimeBodyPart();
        //图片需要经过数据化的处理
        DataHandler dh=new DataHandler(new FileDataSource("F:\\Project\\JavaWeb\\Mailing\\src\\1.png"));
        //在part中放入这个处理过图片的数据
        body1.setDataHandler(dh);
        //给这个part设置一个ID名字
        body1.setContentID("bz.jpg");//图片设置ID

        //=================================准备文本==============================
        MimeBodyPart body2=new MimeBodyPart();
        body2.setContent("这是一张正文<img src='cid:bz.jpg'>","text/html;charset=UTF-8");

        //=================================准备附件数据==================================
        MimeBodyPart body3= new MimeBodyPart();
        body3.setDataHandler(new DataHandler(new FileDataSource("F:\\Project\\JavaWeb\\Mailing\\src\\sql.txt")));
        body3.setFileName("sql.txt");//附件设置名字

        //拼装邮件正文内容
        MimeMultipart multipart1=new MimeMultipart();
        multipart1.addBodyPart(body1);
        multipart1.addBodyPart(body2);
        multipart1.setSubType("mixed");

        MimeBodyPart contextText = new MimeBodyPart();
        contextText.setContent(multipart1);

        //拼接附件
        MimeMultipart allFile = new MimeMultipart();
        allFile.addBodyPart(body3);//附件
        allFile.addBodyPart(contextText);//正文
        allFile.setSubType("mixed");//正文和附件都在邮件中，所有类型设置为mixed

        //设置到消息中，保存修改
        message.setContent(allFile);
        message.saveChanges();//保存修改
        return message;
    }
}